package com.cf.controller;

import com.cf.entity.ItemCategory;
import com.cf.entity.Result;
import com.cf.service.ItemCategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @author 59899
 */
@RestController
@CrossOrigin
public class ItemCategoryController {
    @Autowired
    private ItemCategoryService itemCategoryService;

    @RequestMapping("/selectAllCategory")
    public Object selectAllCategory(){
        List<ItemCategory> categoryList = itemCategoryService.selectAllCategory();
        if (categoryList!=null){
            return new Result(200,"查询成功",categoryList);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

    @RequestMapping("/addCategory")
    public Object addCategory(int iid, int[] cid){
        int i = itemCategoryService.addCategory(iid, cid);
        if (i!=0){
            return new Result(200,"添加成功",null);
        }else {
            return new Result(400,"添加失败",null);
        }
    }

    @RequestMapping("/selectAllLikeCname")
    public Object selectAllLikeCname(@RequestParam(defaultValue = "1") int pageNum, String cname) {
        PageInfo<ItemCategory> itemCategoryPageInfo = itemCategoryService.selectAllLikeCname(pageNum, cname);
        if (itemCategoryPageInfo.getList().size()!=0){
            return new Result(200,"查询成功",itemCategoryPageInfo);
        }else {
            return new Result(400,"查不到数据",null);

        }
    }

    @RequestMapping("/deleteCategory")
    public Object deleteCategory(long cid) {
        int i = itemCategoryService.deleteCategory(cid);
        if (i!=0){
            return new Result(200,"删除成功",null);
        }else {
            return new Result(400,"删除失败",null);
        }
    }

    @RequestMapping("/selectItemCategoryByCid")
    public Object selectItemCategoryByCid(long cid) {
        ItemCategory itemCategory = itemCategoryService.selectItemCategoryByCid(cid);
        if (itemCategory!=null) {
            return new Result(200,"查询成功",itemCategory);
        }else {
            return new Result(400,"查询不到数据",null);
        }
    }

    @RequestMapping("/updateCategoryByCid")
    public Object updateCategoryByCid(ItemCategory itemCategory) {
        int i = itemCategoryService.updateCategoryByCid(itemCategory);
        if (i!=0){
            return new Result(200,"修改成功",null);
        }else {
            return new Result(400,"修改失败",null);
        }
    }

}
